//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace Shp.Runtime.DataModel.Sitka.Tests
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Shp.Runtime.Sitka.ServiceAgent.Entities;
    using Shp.Runtime.Sitka.ServiceAgent.EntityMappers;
    using Shp.Runtime.Sitka.ServiceAgent.Managers;
    using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;
    
    [TestClass]
    public class SystemDataModelFixture
    {
        private MetadataInitialize metadata;
        private TestableRepositoryFactory factory;
        
        [TestInitialize]
        public void Initialize()
        {
            metadata = new MetadataInitialize();

            factory = new TestableRepositoryFactory(metadata.TenantId);            
            factory.CreateContainers();

            metadata.Container = factory.MetadataContainerName;
            metadata.CreateTenant();
        }

        [TestCleanup]
        public void TestCleanUp()
        {
            factory.DeleteContainers();
        }

        [TestMethod]
        public void ShouldGetTenantId()
        {
            SystemDataModel tenantAccess = new SystemDataModel(factory);
            Guid retrievedTenantId = tenantAccess.GetTenantId(metadata.TenantAlias);
            Assert.AreEqual(metadata.TenantId, retrievedTenantId);
        }

        [TestMethod]
        public void ShouldNotGetTenantIdForUnexistingTenant()
        {
            SystemDataModel tenantAccess = new SystemDataModel(factory);
            Guid retrievedTenantId = tenantAccess.GetTenantId(Guid.NewGuid().ToString());
            Assert.AreEqual(Guid.Empty, retrievedTenantId);
        }

        [TestMethod]
        public void ShouldGetTenantAlias()
        {
            SystemDataModel tenantAccess = new SystemDataModel(factory);
            string retrievedTenantAlias = tenantAccess.GetTenantAlias(metadata.TenantId);
            Assert.AreEqual(metadata.TenantAlias, retrievedTenantAlias);
        }

        [TestMethod]
        public void ShouldGetAllTenantIds()
        { 
            Guid tenant1 = Guid.NewGuid();
            Guid tenant2 = Guid.NewGuid();
            this.metadata.AddTenant(tenant1.ToString(), "tenantAlias1");
            this.metadata.AddTenant(tenant2.ToString(), "tenantAlias1");

            SystemDataModel tenantAccess = new SystemDataModel(factory);
            Guid[] tenantIds = tenantAccess.GetAllTenantIds();

            Assert.IsNotNull(tenantIds);
            Assert.AreEqual(3, tenantIds.Length);            
        }

        class MockClass : ExtensibleEntity
        {
            public MockClass(Guid id)
                : base()
            {
                this.Id = id;
            }
        }

        class MockClassMapper : EntityMapper<MockClass>
        {
            public override MockClass ToType(Entity flexibleEntity)
            {
                MockClass myObject = new MockClass(new Guid(flexibleEntity.Id));
                return myObject;
            }

            public override Entity FromType(MockClass entity)
            {
                Entity EntityMock = EntityManager.CreateEntity(this.Kind, entity.Id.ToString());
                return EntityMock;
            }
        }


    }
}
